import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:industry_net/base/component/hud/controller.dart';
import 'hud_view.dart';

abstract class ILoaddingHud {}

class _LoaddingHud extends ILoaddingHud {
  static final _LoaddingHud instance = _LoaddingHud._();
  factory _LoaddingHud() {
    return instance;
  }
  _LoaddingHud._() {
    hudKey = GlobalKey();
    controller = HudController();
  }
  late GlobalKey hudKey;
  late HudController controller;
  DateTime? _lastTime;
  List<String> toasts = [];
  int duration = 1500;
  int timeout = 10000;

  bool get isShow {
    return controller.isShowActive.value || controller.isShowToast.value;
  }
}

// ignore: non_constant_identifier_names
ILoaddingHud get Hud => _LoaddingHud.instance;

extension LoaddingHud on ILoaddingHud {
  void show({
    String? status,
  }) async {
    if (_LoaddingHud.instance._lastTime != null) {
      if (_LoaddingHud.instance.controller.isShowActive.value) {
        return;
      }

      if (_LoaddingHud.instance.controller.isShowToast.value) {
        var diff = DateTime.now().difference(_LoaddingHud.instance._lastTime!);
        Future.delayed(diff, () {
          show(status: status);
        });
        return;
      }
    }
    _LoaddingHud.instance.controller.isShowActive.value = true;
    _LoaddingHud.instance._lastTime = DateTime.now();
    showHudView(status: status, isActive: true);
    await Future.delayed(Duration(milliseconds: _LoaddingHud.instance.timeout));
    dismiss();
  }

  void toast(String status) async {
    if (status.isEmpty) {
      return;
    }
    if (_LoaddingHud.instance.controller.isShowToast.value) {
      _LoaddingHud.instance.toasts.add(status);
      return;
    }
    _LoaddingHud.instance.controller.toast.value = status;
    _LoaddingHud.instance.controller.isShowToast.value = true;

    if (_LoaddingHud.instance.hudKey.currentWidget == null &&
        !_LoaddingHud.instance.controller.isShowActive.value) {
      showHudView(isActive: false, status: status);
    }
    _LoaddingHud.instance.controller.isShowActive.value = false;
    await Future.delayed(
        Duration(milliseconds: _LoaddingHud.instance.duration));
    _LoaddingHud.instance.toasts.remove(status);

    if (_LoaddingHud.instance.toasts.isNotEmpty) {
      _LoaddingHud.instance.controller.isShowToast.value = false;
      toast(_LoaddingHud.instance.toasts.first);
    } else {
      dismiss();
    }
  }

  void dismiss() {
    if (_LoaddingHud.instance.hudKey.currentWidget == null) {
      return;
    }
    if (!_LoaddingHud.instance.isShow) {
      return;
    }
    if (_LoaddingHud.instance.toasts.isNotEmpty) {
      return;
    }
    _LoaddingHud.instance.controller.isShowToast.value = false;
    _LoaddingHud.instance.controller.isShowActive.value = false;
    Get.back();
  }

  void showHudView({
    String? status,
    required bool isActive,
  }) {
    if (Get.context == null) {
      return;
    }
    _LoaddingHud.instance.controller.isShowActive.value = isActive;
    showDialog(
        context: Get.context!,
        barrierDismissible: false,
        barrierColor: null,
        builder: (context) {
          return HudView(
            key: _LoaddingHud.instance.hudKey,
            controller: _LoaddingHud.instance.controller,
          );
        });
  }
}
